package ch.unifr.diuf.webeng.taskmap.server;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import ch.unifr.diuf.webeng.taskmap.client.LoginService;
import ch.unifr.diuf.webeng.taskmap.db.TaskMapDAO;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class LoginServiceImpl extends RemoteServiceServlet implements LoginService{
	HttpServletRequest request;
    HttpSession session;

	private static final long serialVersionUID = 8326963106729768731L;

	@Override
	public String loginUser(String userName, String password) {
		System.out.println("Entering login user");
		String existingUser = "bah";
		this.request = this.getThreadLocalRequest();
		this.session=this.request.getSession();
		TaskMapDAO dao = TaskMapDAO.getInstance();
		existingUser = dao.getRegisteredUser(userName, password);
//		if(existingUser != null){
//			session.setAttribute("userName", existingUser);
//		}
		session.setAttribute("userName", existingUser);
//		System.out.println(session.getAttribute("userName"));
//		session.invalidate();
//		System.out.println(session.getAttribute("userName"));
		System.out.println("Exiting logging user" + existingUser);
		return existingUser;
	}

	@Override
	public String logout() {
		HttpSession session=getThreadLocalRequest().getSession();
		String loggedUser = (String) session.getAttribute("userName");
		session.invalidate();
		return loggedUser;
	}

}
